#include "music_task.h"

/**
 * 文件名：Music_task.c
 * 作者：JunrLi
 * 描述：开发板蜂鸣器响应
 *      1.开机播放wait_wind一次.
 *      2...
 * 修改人：--
 * 修改时间：--
 * 修改内容：--
 */
#include "music_task.h"
#include "cmsis_os.h"
#include "main.h"
#include "tim.h"
#include "remote_control.h"

eMODE mode;
extern sbus_rc_t sbus_rc;
// uint16_t wind_rise[] = {M2,50,M2,50,M1,25,M2,50,M2,50,M1,25,M2,50,M3,50,M5,50,M3,50, M2,50,M2,50,M3,25,M2,50,M1,50,L6,100,Z0,10};

//         uint16_t wait_wind[] = {M2,30,M3,30,M5,30,M3,30,M2,60,M1,30,M3,30,Z0,30,
//                                 M2,30,M3,30,M5,30,M3,30,M2,30,M3,50,M5,30,Z0};

//       uint16_t wait_wind[] = {M1,30,M2,30,M3,30,M5,30,M7,30,H1,60,M5,30,Z0,30,
//                               M5,30,M7,30,H1,60,M5,30,Z0,30};
uint16_t wait_wind[]  = {M7,30,H1,60,M5,30,Z0,30}; //自动运动模式提示
uint16_t closeMusic[] = {M5,30,M3,30,M2,30,Z0,30}; //锁定运动模式提示
uint16_t openMusic[]  = {M2,30,M3,30,M5,30,Z0,30}; //正常运动模式提示
uint16_t errorMusic[] = {M1,60,M1,60,M1,60,M1,60}; //错误报警提示

void music_task(void const *pvParameters)
{
    static uint8_t numClose,numOpen,numAuto = 0;
    music_task_Init();
    osDelay(400);
    while(1){
        rc_music();
        switch (mode) {
            case CLOSE_MODE:
                close_Beep();
                break;
            case OPEN_MODE:
                open_Beep();
                break;
            case AUTO_MODE:
                auto_Beep();
                break;
            case STOP_MODE:
                stop_Beep();
            case ERROR_MODE:
                error_Beep();
                break;
            default:;
        }
        osDelay(100);
    }
}

void music_task_Init(void)
{
    HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
    USER_LED1_OFF();
    USER_LED2_OFF();
}

void rc_music(void)
{
    static int16_t lastRcVlue[2] = {0};
    if (sbus_rc.sw[0] != lastRcVlue[0]) {
        if (sbus_rc.sw[0] == 517) {
            mode = AUTO_MODE;
            USER_LED1_ON();
            USER_LED2_OFF();
            lastRcVlue[0] = sbus_rc.sw[0];
        }
        else if (sbus_rc.sw[0] == 0) {
            mode = OPEN_MODE;
            USER_LED1_OFF();
            USER_LED2_ON();
            lastRcVlue[0] = sbus_rc.sw[0];
        } else if (sbus_rc.sw[0] == -513) {
            mode = CLOSE_MODE;
            USER_LED1_OFF();
            USER_LED2_OFF();
            lastRcVlue[0] = sbus_rc.sw[0];
        }
    }
}

void close_Beep(void){
    static uint8_t numClose;
        for (int i = 0; i < 4; i++) {
            __HAL_TIM_PRESCALER(&htim4, closeMusic[i * 2]);
            __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, 512);
            HAL_Delay(closeMusic[i * 2 + 1] * 7);
            numClose++;
        }
    if (numClose >= 4) mode = STOP_MODE;
}

void open_Beep(void){
    static uint8_t numOpen;
        for (int i = 0; i < 4; i++) {
            __HAL_TIM_PRESCALER(&htim4, openMusic[i * 2]);
            __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, 512);
            HAL_Delay(openMusic[i * 2 + 1] * 7);
            numOpen++;
        }
    if (numOpen >= 4) mode = STOP_MODE;
}

void auto_Beep(void){
    static uint8_t numAuto;
    for (int i = 0; i < 4; i++) {
                __HAL_TIM_PRESCALER(&htim4, wait_wind[i * 2]);
                __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, 512);
        HAL_Delay(wait_wind[i * 2 + 1] * 7);
        numAuto++;
    }
    if (numAuto >= 4) mode = STOP_MODE;
}

void stop_Beep(void){
    __HAL_TIM_PRESCALER(&htim4, Z0);
    __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, 50);
}

void error_Beep(void){
    for (int i = 0; i < 4; i++) {
                __HAL_TIM_PRESCALER(&htim4, errorMusic[i * 2]);
                __HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_1, 512);
        HAL_Delay(errorMusic[i * 2 + 1] * 7);
    }
}
